package com.zmsoft.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @auther zmsoft
 * @Created 2021/8/19 14:38
 */
public class ThreadTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        }, executor);

        //方法执行完成后的感知
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).whenComplete((res,exception)->{
//            //虽然能得到异常信息，但是没法修改返回数据。
//            System.out.println("异步任务成功完成了...结果是："+res+";异常是："+exception);
//        }).exceptionally(throwable -> {
//            //可以感知异常，同时返回默认值
//            return 10;
//        });

        //方法执行完成后的处理
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).handle((res,thr)->{
//            if (res!=null) {
//                return res*2;
//            }
//            if (thr!=null) {
//                return 0;
//            }
//            return 0;
//        });

        /**
         * 线程串行化
         * 1）、thenRun: 不能获取到上一步的执行结果
         *  .thenRunAsync(() -> {
         *      System.out.println("任务2启动了...");
         *  },executor)
         *  2）、thenAcceptAsync; 能接收上一步结果，但是无返回值
         *  3）、thenApplyAsync; 能接收上一步结果，有返回值
         */
//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).thenRunAsync(() -> {
//            System.out.println("任务2启动了...");
//        }, executor);
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).thenAcceptAsync(res->{
//            System.out.println("任务2启动了...");
//        },executor);
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).thenApplyAsync(res -> {
//            System.out.println("任务2启动了...." + res);
//            return "hello" + res;
//        }, executor);
//
//        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务1结束：" + i);
//            return i;
//        }, executor);
//
//        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务2结束：" + i);
//            return "Hello";
//        }, executor);

//        future01.runAfterBothAsync(future02, ()->{
//            System.out.println("任务3线程：" + Thread.currentThread().getId());
//        },executor);
//        future01.thenAcceptBothAsync(future02, (f1,f2)->{
//            System.out.println("任务4线程：" + Thread.currentThread().getId());
//        }, executor);

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ": " + f2 + " -> Hello";
//        }, executor);

        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务1结束：" + i);
            return i;
        }, executor);

        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束：" + i);
            return "Hello";
        }, executor);

        /**
         * 两个任务只要有一个完成，就执行任务3
         * 1)、runAfterEitherAsync ： 不感知结果，自己也无返回值
         * 2)、acceptEitherAsync   ：  感知结果，自己没有返回值
         * 3)、applyToEitherAsync  ：  感知结果，有自己返回值
         */
//        future01.runAfterEitherAsync(future02,()->{
//            System.out.println("任务3线程：" + Thread.currentThread().getId());
//        },executor);
//        future01.acceptEitherAsync(future02,(res)->{
//            System.out.println("任务3线程：" + Thread.currentThread().getId());
//        },executor);
//        future01.applyToEitherAsync(future02,(res)->{
//            System.out.println("任务3线程：" + Thread.currentThread().getId());
//            return res.toString()+"->haha";
//        },executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        });
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性信息");
            return "黑色+256G";
        });
        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的介绍信息");
            return "华为";
        });

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        allOf.get(); //等待所有结果都完成

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        anyOf.get(); //等待所有结果都完成

        System.out.println("main...end...");
    }
}
